/* eslint-disable */
import { KeyValueStore } from '@/components/interface/grid-item-data-interface'
import { isArray } from 'lodash'
import { ChartDetailsEnum } from './contant'

export function getWidgetJson(
  dataSetting: KeyValueStore,
  chartType?: string[] | string
) {
  if (
    (Array.isArray(chartType) && chartType[0] === 'filterForm') ||
    chartType === 'filterForm'
  ) {
    return {}
  }
  if (
    (Array.isArray(chartType) &&
      chartType[1] === ChartDetailsEnum.geographicMap) ||
    chartType === ChartDetailsEnum.geographicMap
  ) {
    return getGeoWidgetJson(dataSetting)
  }
  /* 度量 */
  const values = []
  if (isArray(dataSetting.valueKey)) {
    dataSetting.valueKey.forEach((valueItem: KeyValueStore) => {
      if (valueItem.value !== '') {
        values.push({
          col: valueItem.value,
          sort: valueItem.sort,
          aggregate_type: valueItem.func ? valueItem.func : 'sum',
          topN: dataSetting.topK ? dataSetting.topK : '',
        })
      }
    })
  } else {
    values.push({
      col: dataSetting.valueKey,
      sort: '',
      aggregate_type: dataSetting.aggregateType,
      topN: dataSetting.topK ? dataSetting.topK : '',
    })
  }
  const keys = [
    {
      col: dataSetting.labelKey,
      values: '',
      filterType: '',
      sort: dataSetting.labelSort ? dataSetting.labelSort : 'asc',
    },
  ]
  if (dataSetting.labelKeyY) {
    keys.push({
      col: dataSetting.labelKeyY,
      values: '',
      filterType: '',
      sort: dataSetting.labelSortY ? dataSetting.labelSortY : '',
    })
  }
  return {
    isAggr: !!dataSetting.isAggr,
    config: {
      keys: keys,
      values: values,
      groups: dataSetting.groupBy
        ? [
            {
              col: dataSetting.groupBy,
              values: [],
            },
          ]
        : undefined,
      filters: [],
      topN: dataSetting.topK,
    },
  }
}

/**
 * 适用于geomap处理逻辑的widgetJson
 * @param dataSetting chartOptions
 * @returns widgetJson
 */
function getGeoWidgetJson(dataSetting: KeyValueStore) {
  const keys: any[] = []
  const values: any[] = []
  const { labelKey, valueKey, labelSort } = dataSetting
  ;(Array.isArray(labelKey) ? labelKey : [labelKey]).forEach((key) => {
    keys.push({
      col: key,
      values: '',
      filterType: '',
      sort: labelSort ?? '',
    })
  })
  ;(Array.isArray(valueKey) ? valueKey : [valueKey]).forEach((key) => {
    values.push({
      col: key,
      sort: 'asc',
      aggregate_type: dataSetting.aggregateType,
      topN: dataSetting.topK ? dataSetting.topK : '',
    })
  })
  return {
    isAggr: !!dataSetting.isAggr,
    config: {
      keys: keys,
      values: values,
      groups: dataSetting.groupBy
        ? [
            {
              col: dataSetting.groupBy,
              values: [],
            },
          ]
        : undefined,
      filters: [],
      topN: dataSetting.topK,
    },
  }
}
